const Cache = function() {
  this.caches = {}
}

Cache.prototype.set = function(key, value, expiresIn) {
  this.caches[key] = {
    value,
    expiresIn,
    createTime: new Date().getSeconds()
  }
}

Cache.prototype.get = function(key) {
  const cacheItem = this.caches[key]
  if (!cacheItem) {
    return null
  }

  const isExpiresIn = new Date().getSeconds() - cacheItem.createTime >= cacheItem.expiresIn
  if (isExpiresIn) {
    return null
  }
  return cacheItem.value
}

Cache.getInstance = (function() {
  let instance = null

  return function() {
    if (!instance) {
      instance = new Cache()
    }

    return instance
  }
})()

module.exports = Cache